import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class InventoryList extends HttpServlet
{
	Connection con;

	
	protected void doGet(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException
	{
		 Inventory(req, res);
    }
    
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException
  {
	 if(req.getParameter("delete") != null)
    {
		 System.out.println(req.getParameter("itemcode"));
	    deletePage(res,req.getParameter("itemcode"));
    }
	 else
	 {
		 res.sendRedirect("InventoryList");
	 }
}
	protected void Inventory(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException
	{
		PrintWriter out = res.getWriter();
		
		String itemId = null;
		String itemCode = null;
		String itemName = null;
		String itemDetail = null;
		String categoryCode = null;
		String itemQuantity = null;
		
		try
		{
			DbConnection connection = new DbConnection();
			Class.forName(connection.getDriver());
			con = connection.getConnection();
			Statement stmt = con.createStatement();
			ResultSet rs=stmt.executeQuery(connection.getQuery());
			
			out.println("<html><head><title>Online Inventory System - List Items</title></head>");
			out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />");
			out.print("<SCRIPT language=JavaScript src=\"sortable.js\" type=text/javascript></SCRIPT>");
			out.print("<div id=\"outer\">"
					+" <div id=\"wrapper\">"
					+" <div id=\"nav\">"
					+" <div id=\"nav-left\">"
					+" <div id=\"nav-right\">");
			out.print("<ul>"
	            +"<li><a href=index.jsp>Home</a></li>"
				+"<li><a href=InventoryList>List Items</a></li>"
	            +"<li><a href=addItem.jsp>Add New Item</a></li>"
	            +"<li><a href=CategoryList>List Categories</a></li>"
	            +"<li><a href=addCategory.jsp>Add New Category</a></li>"	            	            
	            +" </ul>");
			out.print("</div>");
			out.print("</div>");
			out.print("<div class=\"clear\"></div>");
			out.print("</div>");
			out.print("<div id=\"head-2\"></div>"
					+"<div id=\"login\">"
					+"<div id=\"login-bot\">");
			
			out.print("<table class=sortable id=sortable_example>");
			out.print("<tr><th>Item Code</th><th>Item Name</th><th>Item Detail</th><th>Category Code</th><th>Quantity</th></tr>");
				while(rs.next()) 
				{
					itemId = rs.getString(1).trim();//item table PK
					itemCode = rs.getString(2).trim();
					itemName = rs.getString(3).trim();
					itemDetail = rs.getString(4).trim();
					categoryCode = rs.getString(5).trim();
					itemQuantity = rs.getString(6).trim();
					out.println("<tr><td>"+ itemCode +"</td>");
					out.println("<td>" +itemName + "</td>");
					out.println("<td>" +itemDetail + "</td>");
					out.println("<td>" +categoryCode + "</td>");
					out.println("<td>" +itemQuantity + "</td>");
					out.println("<td><a href=UpdateItem?itemcnt="+itemCode+">update</a></td>");
					out.println("<td><form method=POST><input type=hidden name=itemcode value=\""+itemCode+"\"/><INPUT TYPE=SUBMIT NAME=\"delete\" VALUE=\"delete\"/></form></td></tr>");
				}
				

				out.print("</body></html>");		
		}
		catch (Exception e)
		{
			out.println("<html><body background color=red>"+e+"</body></html>");
		}
		
	}
	
private void deletePage(HttpServletResponse res,String itemcode)
  throws ServletException, IOException
  {
		try
    	{  
    		DbConnection connection = new DbConnection();

	    	Class.forName(connection.getDriver());
    		con = connection.getConnection();

    	
    		
    		PreparedStatement st = con.prepareStatement("delete from item where item_code=?");
    		st.setString(1, itemcode);
    		int pe = st.executeUpdate();
    		System.out.println("Delete Success");
    		
    		
    		st.close();
    		con.close();
    		
		}
		catch (Exception e) 
    	{
    		PrintWriter o = res.getWriter();
    		o.println("Error statement");
    		e.printStackTrace();
    	}
		finally
		{
			res.sendRedirect("InventoryList");
		}
  }
}